// { config as config1} 重命名
import { config} from '../config.js'
const tips = {
  1:'抱歉，出现一个错误',
  1005:'appkey无效',
  3000:"期刊不存在",
  1007:"链接出错"
}

class HTTP{
  request({url,data={},method="GET"}){
    return new Promise((resolve, reject) => {
      this._request(url, resolve, reject, data,method)
    })

  }
  _request(url, resolve, reject, data={},method="GET"){
    wx.request({
      url: config.api_blink_url+url,
      method:method,
      data:data,
      header:{
        'content-type':'application/json',
        'appkey':config.appkey
      },
      success:(res)=>{
        const code=res.statusCode.toString()
        if (code.startsWith('2')){
          resolve(res.data)
        }else{
          reject()
          console.log("1res", res)
          let error_code = res.data.error_code
          this._show_error(error_code)
        }
      },
      fail:(err)=>{
        reject()
        console.log("2err",err)
        this._show_error(1)
      }

    })
  }
  _show_error(error_code){
    console.log("error_code",error_code)
    if (!error_code) { error_code=1}
    const tip = tips[error_code]
    wx.showToast({
      title: tip ? tip : tip[1],
      icon: "none",
      duration: 2000
    })
  }
}
export {HTTP}